<?php
/**
 * 获取病历列表
 * Created by PhpStorm.
 * User: zhang-hong
 * Date: 2017/3/8
 * Time: 9:58
 */
class GetbinglilistController extends Yaf_Controller_Abstract
{
    function IndexAction()
    {
        $request = $this->getRequest();
        $zhuyuan_id = $request->getPost("zhuyuan_id",0);
        $yiyuan_id = $request->getPost("yiyuan_id",0);
        $zhuyuaninfomodel = new ZhuyuanBingshiModel();
        $data = array();
        if(!ctype_digit("$yiyuan_id"))
        {
            Utility::apiJsonError(20027, '医院ID必须是整型');
        }
        if(!$zhuyuan_id)
        {
            Utility::apiJsonError(20014, '住院ID不能为空');
        }
        if(empty($zhuyuan_id))
        {
            Utility::apiJsonError(20013, '患者信息有误');
        }
        else
        {
            // 获取住院基本信息
            $zhuyuan_basic_info = $zhuyuaninfomodel->getZhuYuanBasicInfo($yiyuan_id,$zhuyuan_id);
            $data['yiliaofukuanfangshi'] = $zhuyuan_basic_info['$zhuyuan_basic_info']==null? '' :$zhuyuan_basic_info['$zhuyuan_basic_info'];
            // 获取入院记录信息(住院病史)
            $zhuyuan_bingshi = $zhuyuaninfomodel->getZhuyuanBingshiInfo($zhuyuan_id,$yiyuan_id);

            // 主治医师
            $zhuzhenyishi = "";
            // 先查询zhuyuan_zongjie_info表
            $zhuyuan_zongjie_info = $zhuyuaninfomodel->getZhuYuanZongJie($zhuyuan_id,$yiyuan_id);
            if(!empty($zhuyuan_zongjie_info["zhuyuanyishi_id"]))
            {
                $zhuzhenyishi = $zhuyuan_zongjie_info["zhuyuanyishi_name"]."（".$zhuyuan_zongjie_info["zhuyuanyishi_id"]."）";
            }

            // 若查询结果为空再查询zhuyuan_basic_info表
            if(empty($zhuzhenyishi))
            {
                $zhuzhenyishi = $zhuyuan_basic_info["zhuzhenyishi"];
                if(!empty($zhuyuan_basic_info["zhuzhenyishi_code"]))
                {
                    $zhuzhenyishi = $zhuzhenyishi."（".$zhuyuan_basic_info["zhuzhenyishi_code"]."）";
                }
            }
            $data["zhuzhenyishi"] = strip_tags($zhuzhenyishi);
            $data["zerenhushi"] = strip_tags($zhuyuan_basic_info["zerenhushi"]);

            // 主诉
            $zhusu = "";
            // 先查询zhuyuan_bingshi表
            $zhusu = $zhuyuan_bingshi["zhusu"];

            // 若查询结果为空再查询zhuyuan_basic_info表
            if(empty($zhusu))
            {
                $zhusu = $zhuyuan_basic_info["zhusu"];
            }

            $data["zhusu"] = strip_tags($zhusu);

            // 诊断
            $zhenduan = '';
            // 先查询诊断表（从老版本拷贝诊断代码）
            $zhuyao_zhenduan_xiyi = $zhuyuaninfomodel->GetZhuZhiYishi($zhuyuan_id,$yiyuan_id);
            if(!empty($zhuyao_zhenduan_xiyi))
            {
                $zhenduan .= '西医【主要诊断：'.$zhuyao_zhenduan_xiyi['zhenduan_mingcheng'];
            }
            $ciyao_zhenduan_xiyi = $zhuyuaninfomodel->GetZhenDuanXiyi($zhuyuan_id,$yiyuan_id);
            if(!empty($ciyao_zhenduan_xiyi))
            {
                $zhenduan .="   次要诊断:";
                foreach ($ciyao_zhenduan_xiyi as $ciyao_zhenduan_xiyi_one )
                {
                    $zhenduan .=$ciyao_zhenduan_xiyi_one['zhenduan_mingcheng'].'、';
                }
            }
            if(!empty($zhuyao_zhenduan_xiyi) or !empty($ciyao_zhenduan_xiyi))
            {

                $zhenduan .='】';
            }

            $zhuyao_zhenduan_zhongyi = $zhuyuaninfomodel->GetZhongYiZhenDuan($zhuyuan_id,$yiyuan_id);
            if(!empty($zhuyao_zhenduan_zhongyi))
            {
                $zhenduan .= '中医【主要诊断：'.$zhuyao_zhenduan_zhongyi['zhenduan_mingcheng'];
            }
            $zhongyi_bingzheng = $zhuyuaninfomodel->GetZhongYiBingzheng($zhuyuan_id,$yiyuan_id);
            if(!empty($zhongyi_bingzheng))
            {
                $zhenduan .="   中医病症:";
                foreach ($zhongyi_bingzheng as $zhongyi_bingzheng_one )
                {
                    $zhenduan .=$zhongyi_bingzheng_one['zhenduan_mingcheng'].'、';
                }
            }
            if(!empty($zhuyao_zhenduan_zhongyi) or !empty($zhongyi_bingzheng))
            {
                $zhenduan .='】';
            }

            // 若查询结果为空再查询zhuyuan_basic_info表
            if(empty($zhenduan))
            {
                $zhenduan = $zhuyuan_basic_info["zhenduan"];
            }

            $data["zhenduan"] = strip_tags($zhenduan);

            // 入院记录
            if(!empty($zhuyuan_bingshi["bingshicaiji_riqi_time"]))
            {
                $data["ruyuan_jilu"] = "入院记录 （ ".$zhuyuan_bingshi["bingshicaiji_riqi_time"]." ）";
            }
            else
            {
                $data["ruyuan_jilu"] = "";
            }

            // 首次病程
            $zhuyuan_bingchengjilushouci =  $zhuyuaninfomodel->GetShouciBingli($zhuyuan_id,$yiyuan_id);
            if(!empty($zhuyuan_bingchengjilushouci))
            {
                $data["zhuyuan_bingchengjilushouci"] = "首次病程记录 （ ".$zhuyuan_bingchengjilushouci["record_time"]." ）";
            }
            else
            {
                $data["zhuyuan_bingchengjilushouci"] = "";
            }

            //病程记录
//            if(C('bingcheng_jilu_xianshi')=="v2") {
//                $sqls = "select m.bingcheng_id as id,m.bingcheng_sub_leibie,m.record_time from
//				(select a.bingcheng_id,a.bingcheng_sub_leibie,a.record_time from zhuyuan_bingchengjilu a,
//				zhuyuan_basic_info b where a.zhuyuan_id = b.zhuyuan_id and a.zhuyuan_id='$zhuyuan_id' and a.record_time>=b.ruyuan_riqi_time)m
//				group by m.bingcheng_id,m.bingcheng_sub_leibie,m.record_time";
//                $zhuyuan_bingchengjilu = M()->query($sqls);
//            }else {
//                $zhuyuan_bingchengjilu = M('zhuyuan_bingchengjilu')->field('id,bingcheng_sub_leibie,record_time')->where("zhuyuan_id = '$zhuyuan_id' ")->select();
//
//            }
            $zhuyuan_bingchengjilu = $zhuyuaninfomodel->GetBingChengJilu($zhuyuan_id,$yiyuan_id);

            // 如果为空，赋值为空数组
            if(empty($zhuyuan_bingchengjilu))
            {
                $zhuyuan_bingchengjilu = array();
            }

            // 去除空白
            foreach ($zhuyuan_bingchengjilu as $key => $one)
            {
                $zhuyuan_bingchengjilu[$key]["bingcheng_sub_leibie"] = trim($one["bingcheng_sub_leibie"]);
            }
            if(empty($zhuyuan_bingchengjilu))
            {
                $zhuyuan_bingchengjilu = array();
            }
            $data["zhuyuan_bingchengjilu"] = $zhuyuan_bingchengjilu;

            $data["error"] = "200";
            $data["msg"] = "";
        }
        Utility::apiJsonResultExtends($data);
        //Utility::apiJsonResult($data,"",$_REQUEST["compression_http_data"]);
    }

}